<template>
	<div class="ip_address_input" :class="{ disabled }" v-clickoutside="handleClickOutside">
		<template v-for="(num, index) in 4">
			<span class="dot" v-if="index !== 0">.</span>
			<el-input
				:maxlength="3"
				:disabled="disabled"
				:ref="`input${index}`"
				v-model="ipAddress[index]"
				@focus="handleFocusEvent"
				@input="handleInputEvent($event, index)"
				@keyup.native.prevent="handleKeyupEvent($event, index)"
			/>
		</template>
		<template v-if="!!clearable">
			<span class="el-icon-circle-close" :class="{ show: isFocus }" @click.stop="handleClearData()"></span>
		</template>
	</div>
</template>

<script>
import emitter from 'element-ui/src/mixins/emitter';
import Clickoutside from 'element-ui/src/utils/clickoutside';
export default {
	name: 'swIpInput',
	mixins: [emitter],
	props: {
		value: { type: String, default: '' },
		disabled: { type: Boolean, default: false },
		clearable: { type: Boolean, default: false }
	},
	data() {
		return {
			ipString: this.value,
			ipAddress: [],
			isFocus: false
		};
	},
	directives: { Clickoutside },
	watch: {
		value(val) {
			this.ipString = val;
		},
		ipString(val) {
			this.$emit('input', val);
			this.ipAddress = !val ? [] : val.split('.');
			this.dispatch('ElFormItem', 'el.form.change', [val]);
		},
		ipAddress: {
			handler(arr) {
				let ipArrs = arr.filter((n) => !!n);
				this.ipString = ipArrs.join('.');
			},
			deep: true
		}
	},
	methods: {
		handleFocusEvent() {
			this.isFocus = true;
		},

		handleClickOutside() {
			this.isFocus = false;
		},

		handleInputEvent(value, index) {
			let inputValue = value.replace(/[^\d]/g, '');
			let substringValue = inputValue.substring(0, 2);
			if (inputValue > 255) {
				this.ipAddress[index] = substringValue;
			} else if (inputValue.charAt(0) !== '0') {
				this.ipAddress[index] = inputValue;
			} else {
				this.ipAddress[index] = inputValue.slice(-1);
			}
		},

		handleKeyupEvent(event, index) {
			let value = event.target.value;
			if (event.key === '.' && !!value && index < 3) {
				this.$refs[`input${index}`][0].blur();
				this.$refs[`input${index + 1}`][0].focus();
			}
			if (event.key === 'Backspace' && !value && index > 0) {
				this.$refs[`input${index}`][0].blur();
				this.$refs[`input${index - 1}`][0].focus();
			}
		},

		handleClearData() {
			this.ipString = '';
			this.isFocus = false;
		}
	}
};
</script>

<style lang="scss" scoped>
.ip_address_input::v-deep {
	display: inline-flex;
	align-items: center;
	border-radius: 4px;
	border: solid 1px #dcdfe6;
	padding: 0 10px;
	width: 100%;
	overflow: hidden;
	box-sizing: border-box;
	position: relative;
	&.disabled {
		background-color: #f5f7fa;
	}
	&:has(.el-icon-circle-close) {
		padding-right: 30px;
	}
	.el-input {
		flex: 1;
		margin: -1px 0;
		font-size: 14px;
		overflow: hidden;
	}
	.el-input__inner {
		border: none;
		padding: 0;
		text-align: center;
	}
	span {
		padding: 0 6px;
	}
	span.dot {
		font-size: 16px;
		line-height: 20px;
	}
	span.el-icon-circle-close {
		position: absolute;
		top: 50%;
		right: 10px;
		padding: 0;
		color: #c0c4cc;
		cursor: pointer;
		transform: translateY(-50%);
		display: none;
		&.show {
			display: block;
		}
	}
}
</style>
